import { createRouter, createWebHashHistory, createWebHistory, type RouteRecordRaw } from "vue-router";

import LAYOUT from "@/layout/index.vue";
import { PageEnum } from "@/enums/app-enum";

const constantRoutes: Array<RouteRecordRaw> = [
    {
        path: "/",
        redirect: PageEnum.LOGIN
    },
    {
        path: PageEnum.INDEX,
        component: LAYOUT, // 整体布局结构组件
        name: "layout",
        children: [
            {
                path: "",
                // path: "welcome",
                name: "welcome",
                component: () => import("@/views/home/index.vue"),
                meta: {
                    title: "首页"
                }
            },
            {
                path: "chart",
                name: "chart",
                component: () => import("@/views/chart/index.vue"),
                meta: {
                    title: "统计图表"
                }
            },
            {
                path: "user",
                name: "user",
                component: () => import("@/views/user/index.vue"),
                meta: {
                    title: "用户管理"
                }
            },
            {
                path: "list",
                meta: {
                    title: "列表管理"
                },
                children: [
                    {
                        path: "",
                        name: "list",
                        component: () => import("@/views/list/index.vue")
                    },
                    {
                        path: "detail",
                        name: "detail",
                        component: () => import("@/views/list/detail.vue"),
                        meta: {
                            title: "详情"
                        }
                    }
                ]
            }
        ]
    },
    {
        path: PageEnum.LOGIN,
        name: "login",
        component: () => import("@/views/login/index.vue"),
        meta: {
            title: "登录"
        }
    }
    // {
    //     path: PageEnum.REGISTER,
    //     name: "register",
    //     component: () => import("@/views/register/index.vue"),
    //     meta: {
    //         title: "注册"
    //     }
    // }
];

const errorRoutes: Array<RouteRecordRaw> = [
    {
        path: "/:pathMatch(.*)*",
        component: () => import("@/views/error/404.vue"),
        name: "notFound",
        meta: {
            title: "404"
        }
    },
    {
        path: PageEnum.ERROR_403,
        component: () => import("@/views/error/403.vue"),
        name: "forbidden",
        meta: { title: "403" }
    }
];
const router = createRouter({
    history: createWebHashHistory(),
    // history: createWebHistory(),
    routes: [...constantRoutes, ...errorRoutes]
});

const ROUTER_WHITE_LIST: string[] = [PageEnum.LOGIN, PageEnum.REGISTER];

/**
 * @description 路由拦截 beforeEach
 * */
router.beforeEach(async (to, from, next) => {
    // const globalStore = useGlobalStore();

    // 1.NProgress 开始
    // NProgress.start();

    // 2.动态设置标题
    const title = "FilmHub";
    document.title = to.meta.title ? `${title} | ${to.meta.title}` : title;

    // 3.判断是访问登陆页，有 Token 就在当前页面，没有 Token 重置路由并放行到登陆页
    // if (to.path.toLocaleLowerCase() === PageEnum.LOGIN) {
    //     // if (globalStore.token) return next(from.fullPath);
    //     // resetRouter();
    //     return next();
    // }

    // 4.判断访问页面是否在路由白名单地址中，如果存在直接放行
    if (ROUTER_WHITE_LIST.includes(to.path)) return next();

    // if (to.path === "/profile" && !localStorage.getItem("token"))
    //     return next({ path: PageEnum.ERROR_403, replace: true });

    // 5.判断是否有 Token，没有重定向到 login
    // if (!globalStore.token) return next({ path: LOGIN_URL, replace: true });

    // 6.如果没有菜单列表，就重新请求菜单列表并添加动态路由
    // const authStore = useAuthStore();
    // authStore.setRouteName(to.name as string);
    // if (!authStore.authMenuListGet.length) {
    //     /** 此处重新获取菜单列表 */
    //     // await initDynamicRouter();
    //     return next({ ...to, replace: true });
    // }

    // 7.正常访问页面
    next();
});

/**
 * @description 路由跳转结束
 * */
router.afterEach(() => {
    // NProgress.done();
});

export default router;
